package com.blc.service.impl;

import com.alibaba.fastjson.JSON;
import com.blc.config.AccessTokenThreadLocal;
import com.blc.enums.WxEnums;
import com.blc.service.AccessTokenService;
import com.blc.util.InteractionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

@Service
public class AccessTokenServiceImpl implements AccessTokenService {
    private final Logger logger = LoggerFactory.getLogger(AccessTokenServiceImpl.class);
    private final InteractionUtil interactionUtil = new InteractionUtil();

    @Value("${wx.blc.appid}")
    private String appid;

    @Value("${wx.blc.secret}")
    private String secret;

    // 初始化这个bean是就应该跑一次
    @PostConstruct
    public void init(){
        getAccessToken();
    }

    @Override
    public String getAccessToken() {
        String url = WxEnums.ACCESS_TOKEN.getUrl().replace("APPID", appid).replace("APPSECRET", secret);
        String message = interactionUtil.sendGet(url);
        Map map = JSON.parseObject(message, Map.class);
        AccessTokenThreadLocal.set(map.get("access_token")+"");
        return map.get("access_token")+"";
    }
}
